package com.cit.util;

import java.util.Map;

import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.cit.manager.CITWsManager;
import com.cit.service.SoapService;

/**
 * 
 * @author Lang Yu(lang.yu@hp.com)
 * @time Mar 7, 2011 12:13:27 PM
 * @file ResponseUtil.java
 * @package com.cit.util
 * @version 1.0
 * @see This is a tool class to create the format response messages, also it is
 *      the soap format
 */
public class ResponseUtil {
	/**
	 * Create the SoapService Business Interface
	 */
	private static SoapService sService = CITWsManager.newSoapService();

	public static String getObjectResponse(SOAPPart part,
			Map<String, String> map, String namespace, Document respDoc,
			Document inputDoc) throws Exception {
		SOAPEnvelope soapEnvelope = part.getEnvelope();
		SOAPBody soapBody = soapEnvelope.getBody();
		SOAPFactory soapFactory = SOAPFactory.newInstance();
		if (respDoc == null) {
			SOAPElement objectName = soapBody.addChildElement(map.get("action")
					+ "Response", "", namespace);
			// status's value
			SOAPElement status = soapFactory.createElement("status", "",
					namespace);
			status.setTextContent(map.get("status"));
			// error messages
			SOAPElement eMsg = soapFactory.createElement("error_message", "",
					namespace);
			eMsg.setTextContent(map.get("error_message"));
			objectName.addChildElement(status);
			objectName.addChildElement(eMsg);
		} else {
			String inputStr = sService.docToString(respDoc);
			SOAPBody body = sService.getSOAPBody(inputStr);
			Element inputRoot = inputDoc.getDocumentElement();
			Element respRoot = body.extractContentAsDocument()
					.getDocumentElement();
			// Create the response node soap message;
			SOAPElement rootObject = soapBody.addChildElement(respRoot
					.getNodeName().replace("action", map.get("action")), "", namespace);
			NodeList respNodeList = respRoot.getChildNodes();
			for (int i = 0; i < respNodeList.getLength(); i++) {
				Node node = respNodeList.item(i);
				if (node.getNodeType() != Element.TEXT_NODE) {
					String key = node.getTextContent().replace("#{", "")
							.replace("}", "");
					if (key.trim().equals("url") || key.trim().equals("action")
							|| key.trim().equals("status")
							|| key.trim().equals("error_message")) {
						if (key.trim().equals("error_message")) {
							if (map.get("status").trim().equals("error")) {
								SOAPElement singleNode = soapFactory
										.createElement(node.getNodeName(), "", namespace);
								singleNode.setTextContent(map.get(key.trim()));
								rootObject.addChildElement(singleNode);
							}
						} else {
							SOAPElement singleNode = soapFactory.createElement(
									node.getNodeName(), "", namespace);
							singleNode.setTextContent(map.get(key.trim()));
							rootObject.addChildElement(singleNode);
						}
					} else {
						NodeList inputNodeList = inputRoot
								.getElementsByTagName(key.trim());
						if (inputNodeList.getLength() == 1) {
							SOAPElement singleNode = soapFactory.createElement(
									node.getNodeName(), "", namespace);
							singleNode.setTextContent(inputNodeList.item(0)
									.getTextContent());
							rootObject.addChildElement(singleNode);
						}
					}
				}
			}
		}
		return sService.soap2String(part.getContent());
	}
}
